﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Collections;

namespace LiveNet.Contacts
{
    public class LocationCollection
    {
        #region Private Data
        private ArrayList locations = new ArrayList();
        #endregion

        #region Constructors
        internal LocationCollection()
        {
        }

        internal LocationCollection(XmlNode xmlnode)
        {
            FromXml(xmlnode);
        }
        #endregion

        #region Internal Methods
        internal void FromXml(XmlNode xmlnode)
        {
            if (xmlnode == null) return;

            XmlNodeList nodes = xmlnode.SelectNodes("Location");
            if (nodes != null)
            {
                foreach (XmlNode node in nodes)
                {
                    Add(new Location(node));
                }
            }
        }

        internal void Add(Location location)
        {
            locations.Add(location);
        }

        internal void Remove(Location location)
        {
            locations.Remove(location);
        }

        internal void RemoveAll()
        {
            locations.Clear();
        }
        #endregion

        #region Public Properties
        public int Count
        {
            get { return locations.Count; }
        }

        public object[] ToArray
        {
            get { return locations.ToArray(); }
        }
        #endregion

        #region Public Methods
        public XmlDocument ToXml(OperationType op)
        {
            XmlDocument xmldoc = new XmlDocument();
            string xml = "<Locations>";
            foreach (Location location in this)
            {
                xml = xml + location.ToXml(op).InnerXml;
            }
            xml = xml + "</Locations>";
            xmldoc.LoadXml(xml);

            return xmldoc;
        }

        public IEnumerator GetEnumerator()
        {
            return locations.GetEnumerator();
        }

        public Location Find(int locationID)
        {
            foreach (Location location in locations)
            {
                if (location.ID == locationID) return location;
            }
            return null;
        }

        public Location New()
        {
            return new Location();
        }
        #endregion
    }
}
